package com.silverhaze.diamond.client.ui.dialogs;

import com.silverhaze.commons.utils.crypt.MD5;
import com.silverhaze.diamond.common.entity.SystemUser;
import com.silverhaze.diamond.client.settings.SettingsStorage;
import com.silverhaze.diamond.client.ui.MainFrame;
import com.silverhaze.diamond.client.ui.UIHelper;
import com.silverhaze.diamond.client.ui.UIStringConstants;
import com.silverhaze.diamond.client.utils.db.ServiceDataSource;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * SystemUser: Ivan Babanin
 * Date: 11.08.2007
 * Time: 12:08:13
 * This code is part of Lizard Project.
 * Strictly confidential.
 */
public class ChangePasswordDialog extends AbstractDialog {
    private JPasswordField newPasswordField;
    private JPasswordField currentPasswordField;

    private int passwordIncorrectCount;

    private Frame parent;

    public ChangePasswordDialog(Frame parent) {
        super(parent);

        this.parent = parent;
    }

    protected void init(DialogButtonView viewType) {
        super.init(viewType);

        passwordIncorrectCount = 3;

        newPasswordField = new JPasswordField(10);
        currentPasswordField = new JPasswordField(10);

        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(currentPasswordPanel());
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(newPasswordPanel());
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(new JButton(new AbstractAction(UIStringConstants.SAVE) {
            public void actionPerformed(ActionEvent e) {
                if (ChangePasswordDialog.this.isFocused()) {
                    okPressed();
                }
            }
        }));
        hBox.add(new JButton(new AbstractAction(UIStringConstants.CANCEL) {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        }));

        Box vBox = Box.createVerticalBox();
        vBox.add(Box.createVerticalStrut(4));
        vBox.add(hBox);
        vBox.add(Box.createVerticalStrut(4));


        getContent().add(vBox);
        pack();
    }

    private JPanel currentPasswordPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        Box hBox = Box.createHorizontalBox();

        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(new JLabel(UIStringConstants.OLD + ": "));
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(currentPasswordField);
        hBox.add(Box.createHorizontalStrut(5));

        panel.add(hBox);
        return panel;
    }

    private JPanel newPasswordPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        Box hBox = Box.createHorizontalBox();

        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(new JLabel(UIStringConstants.NEW + ": "));
        hBox.add(Box.createHorizontalStrut(5));
        hBox.add(newPasswordField);
        hBox.add(Box.createHorizontalStrut(5));

        panel.add(hBox, BorderLayout.CENTER);
        return panel;
    }

    protected void okPressed() {
        SystemUser currentUser = SettingsStorage.getUserSettings().getCurrentUser();
        String enteredPassword = new String(currentPasswordField.getPassword());
        String newPassword = new String(newPasswordField.getPassword());

        if (!newPassword.equals("")) {
            if (!enteredPassword.equals("") &&
                    currentUser.getPassword().equals(MD5.crypt(enteredPassword))) {
                currentUser.setPassword(MD5.crypt(newPassword));
                ServiceDataSource.getInstance().update(currentUser);

                Dialogs.showMessageDlg(null, UIStringConstants.PASSWORD_SAVED_SUCCESS, UIStringConstants.PASSWORD_SAVED);
                setVisible(false);
            } else {
                passwordIncorrectCount--;

                getErrorPanel().setErrorText(UIStringConstants.INCORRECT_CUR_PASS + passwordIncorrectCount);

                if (passwordIncorrectCount <= 0) {
                    setVisible(false);

                    Dialogs.showErrorDialog(null, UIStringConstants.TOO_MANY_ERRORS);

                    if (parent != null) {
                        parent.setVisible(false);
                        parent.dispose();

                        MainFrame frame = new MainFrame();
                        frame.setVisible(true);
                    }
                }
            }
        } else {
            Dialogs.showErrorDialog(null, UIStringConstants.EMPTY_PASSWORD);
        }
    }

    protected void cancelPressed() {

    }

    public String getCaption() {
        return UIHelper.getCaption() + " - Change password";
    }

    public static void showDlg() {
        ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(null);
        changePasswordDialog.setVisible(true);
    }

    public static void showDlg(Frame parent) {
        ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(parent);
        changePasswordDialog.setVisible(true);
    }

}
